<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="昵称" prop="nickname" >
        <a-input v-model="form.nickname" placeholder="请输入昵称" />
      </a-form-model-item>
      <a-form-model-item label="头像" prop="imageUrl" >
        <file-upload v-model="form.imageUrl" type="image"></file-upload>
      </a-form-model-item>
      <a-form-model-item label="姓名" prop="name" >
        <a-input v-model="form.name" placeholder="请输入姓名" />
      </a-form-model-item>
      <a-form-model-item label="手机号" prop="phone" >
        <a-input v-model="form.phone" placeholder="请输入手机号" />
      </a-form-model-item>
      <a-form-model-item label="年龄" prop="age" >
        <a-input v-model="form.age" placeholder="请输入年龄" />
      </a-form-model-item>
      <a-form-model-item label="openId" prop="openId" >
        <a-input v-model="form.openId" placeholder="请输入openId" />
      </a-form-model-item>
<!--      <a-form-model-item label="密码" prop="password" >-->
<!--        <a-input v-model="form.password" placeholder="请输入密码" />-->
<!--      </a-form-model-item>-->
      <a-form-model-item label="身份" prop="type" >
        <a-select placeholder="请选择身份" v-model="form.type">
          <a-select-option v-for="(d, index) in typeOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="健康管理师" prop="type" v-if="form.type === '0'">
        <a-select placeholder="请选择健康管理师" :filter-option="searchOption" v-model="form.healthUserId" allow-clear showSearch>
          <a-select-option v-for="(d, index) in userLists" :key="index" :value="d.id" >{{ d.nickname }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="状态" prop="status" >
        <a-select placeholder="请选择状态" v-model="form.status">
          <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.value" >{{ d.label }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" :loading="submitLoading" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>
  import { getUser, addUser, updateUser } from '@/api/system/webUser'

  export default {
    name: 'CreateForm',
    props: {
        statusOptions: {
            type: Array,
            required: true
          },
        typeOptions: {
          type: Array,
          required: true
        },
        userLists:{
          type: Array,
          required: true
        }
    },
    components: {
    },
    data () {
      return {
        submitLoading: false,
        formTitle: '',
        // 表单参数
        form: {
            id: undefined,
            nickname: undefined,
            imageUrl: undefined,
            name: undefined,
            phone: undefined,
            age: undefined,
            openId: undefined,
            password: undefined,
            status: '0',
            type:'0',
            healthUserId:undefined,
            delFlag: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined
        },
        // 1增加,2修改
        formType: 1,
        open: false,
        rules: {
          nickname:[
            { required: true, message: '请输入昵称', trigger: 'blur' }
          ],
          imageUrl: [
            { required: true, message: '请上传头像', trigger: 'change' }
          ],
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          phone:[
            { required: true, message: '请输入手机号', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '请输入年龄', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '请选择身份', trigger: 'change' }
          ],
          status:[
            { required: true, message: '请选择状态', trigger: 'change' }
          ]
        }
      }
    },
    filters: {
    },
    created () {
    },
    computed: {
    },
    watch: {
    },
    mounted () {
    },
    methods: {
      searchOption(input,option){
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },
      onClose () {
        this.open = false
      },
      // 取消按钮
      cancel () {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset () {
        this.formType = 1
        this.form = {
            id: undefined,
            nickname: undefined,
            imageUrl: undefined,
            name: undefined,
            phone: undefined,
            age: undefined,
            openId: undefined,
            password: undefined,
            status: '0',
            type:'0',
            healthUserId:undefined,
            delFlag: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined
        }
      },
      /** 新增按钮操作 */
      handleAdd (row) {
        this.reset()
        this.formType = 1
        this.open = true
        this.formTitle = '添加'
      },
      /** 修改按钮操作 */
      handleUpdate (row, ids) {
        this.reset()
        this.formType = 2
        const id = row ? row.id : ids
        getUser(id).then(response => {
          this.form = response.data
          this.open = true
          this.formTitle = '修改'
        })
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.submitLoading = true
            if (this.form.id !== undefined && this.form.id !== null) {
              updateUser(this.form).then(response => {
                this.$message.success('修改成功',3)
                this.open = false
                this.$emit('ok')
              }).finally(() => {
                this.submitLoading = false
              })
            } else {
              addUser(this.form).then(response => {
                this.$message.success('新增成功',3)
                this.open = false
                this.$emit('ok')
              }).finally(() => {
                this.submitLoading = false
              })
            }
          } else {
            return false
          }
        })
      }
    }
  }
</script>
